
package uestc.bitman.edxapp.courses;

import uestc.bitman.edxapp.EdxApp;
import uestc.bitman.edxapp.EdxStudentUser;
import uestc.bitman.edxapp.R;
import uestc.bitman.edxapp.courselist.Course;
import uestc.bitman.edxapp.mainui.EdxAppFragment;
import uestc.bitman.edxapp.mainui.MainActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import java.util.List;

public class CoursesFragment extends EdxAppFragment implements SwipeRefreshLayout.OnRefreshListener {

    /**
     * Progress dialog of loading
     */
    private ProgressDialog mProgressDlg;

    private ListView mCoursesListView;

    private Context mContext;

    private CoursesAdapter mCoursesAdapter;

    private SwipeRefreshLayout mSwipeRefreshLayout;

    private AboutCourseFragment aboutCourseFragment;

    /**
     * @category Constructor
     */
    public CoursesFragment() {

    }

    /**
     * @category onCreate
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mCoursesAdapter = new CoursesAdapter(mContext);
    }

    /**
     * @category Initialize UI here
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        // when switch to this page,update the title and enable the press again
        // to exit(because this page is at home)

        View view = initLayout(inflater, container, savedInstanceState);

        return view;
    }

    /**
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    private View initLayout(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.course_fragment, container, false);

        mCoursesListView = (ListView) view.findViewById(R.id.list_course);
        mCoursesListView.setAdapter(mCoursesAdapter);
        mCoursesListView.setOnItemClickListener(new OnClickCourseListener());

        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        initCourses();
        mProgressDlg = ProgressDialog.show(mContext, getString(R.string.title_processing),
                getString(R.string.label_content_processing));

        return view;
    }

    private void initCourses() {
        CoursesAsyncTask coursesAsyncTask = new CoursesAsyncTask();
        coursesAsyncTask.execute(EdxApp.getEdxAppUser());
        mSwipeRefreshLayout.setRefreshing(true);
    }

    /**
     * @author hackeris
     * @category Load all courses list
     */
    class CoursesAsyncTask extends AsyncTask<EdxStudentUser, Void, List<Course>> {

        @Override
        protected List<Course> doInBackground(EdxStudentUser... params) {
            EdxStudentUser user = params[0];
            return user.getCourses();
        }

        protected void onPostExecute(List<Course> courseList) {
            mProgressDlg.dismiss();
            mSwipeRefreshLayout.setRefreshing(false);

            if (courseList == null || courseList.size() == 0) {
                return;
            }

            mCoursesAdapter.reloadCourses(courseList);
            mCoursesAdapter.notifyDataSetChanged();
        }
    }

    class OnClickCourseListener implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {

            if (aboutCourseFragment == null) {
                aboutCourseFragment = new AboutCourseFragment();
            }
            aboutCourseFragment.setCourse(mCoursesAdapter.getItem(pos));
            startFragment(aboutCourseFragment, R.id.content_frame, "aboutcourse", true);
        }
    }

    /**
     * @category on swipe down refresh
     */
    @Override
    public void onRefresh() {
        initCourses();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return false;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    }
}
